package com.tionsy.springbootcomponents.ocr;

import lombok.SneakyThrows;

import java.net.URLEncoder;

/**
 * @author tianyun
 * @version 1.0.0
 * @date 2022-02-09
 */
public class Test {

    private static String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";

    @SneakyThrows
    public static void main(String[] args) {
        String s = generalBasic();
    }

    public static String generalBasic() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate";
        try {
            // 本地文件路径
            String filePath = "/Users/renren/Downloads/RC - tql.pdf";
            byte[] pdfData = FileUtil.readFileByBytes(filePath);
            String pdfStr = Base64Util.encode(pdfData);
            String base64File = URLEncoder.encode(pdfStr, "UTF-8");
            String param = "pdf_file=" + base64File;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
